package com.taylor.nio;




import org.testng.annotations.Test;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;



/**
 * @Desc : 文件管道demo  FileChannel+ByteBuffer实现
 * @Author ： taylor
 * @Date ：2021/6/26 15:56
 */
public class NIOFileChannel {

    @Test
    public  void test1() {
        String str = "nio是学习netty的基础!";

        try (FileOutputStream fileOutputStream = new FileOutputStream(new File("file/fileChannel.txt"))) {
            FileChannel channel = fileOutputStream.getChannel();
//            byte[] buffer = new byte[1024];
            ByteBuffer buffer = ByteBuffer.allocate(1024);

            buffer.put(str.getBytes());
            buffer.flip();
            channel.write(buffer);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void  test2(){
        //创建文件的输入流
        File file = new File("file/fileChannel.txt");
        try (FileInputStream fileInputStream = new FileInputStream(file)) {
            //通过 fileInputStream 获取对应的 FileChannel -> 实际类型 FileChannelImpl
            FileChannel fileChannel = fileInputStream.getChannel();
            //创建缓冲区
            ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());
            //将通道的数据读入到 Buffer
            fileChannel.read(byteBuffer);
            //将 byteBuffer 的字节数据转成 String
            System.out.println(new String(byteBuffer.array()));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 文件读写
     */
    @Test
    public void test3(){
        try (FileInputStream fileInputStream = new FileInputStream("file/fileChannel.txt");
             FileOutputStream fileOutputStream = new FileOutputStream("file/fileChannel_bak.txt");) {
            FileChannel fileChannel01 = fileInputStream.getChannel();
            FileChannel fileChannel02 = fileOutputStream.getChannel();

            ByteBuffer buffer = ByteBuffer.allocate(1024);
            int readLen = 0;
            while (readLen != -1){
                //这里有一个重要的操作
                buffer.clear();
                readLen = fileChannel01.read(buffer);

                buffer.flip();

                fileChannel02.write(buffer);

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 拷贝
     */
    @Test
    public void test4(){
        //创建相关流
        try (FileInputStream fileInputStream = new FileInputStream("file/jvm.jpg");
             FileOutputStream fileOutputStream = new FileOutputStream("file/jvm-bak.jpg")) {
            //获取各个流对应的 FileChannel
            FileChannel src = fileInputStream.getChannel();
            FileChannel dest = fileOutputStream.getChannel();

            //使用 transferForm 完成拷贝
            dest.transferFrom(src, 0, src.size());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
